if (!exists("app_root"))
    {
    stop("Need to have app_root defined to source this template");
    };

source( paste( app_root, "cgi-lib/R/ep.io.R", sep="/" ) );
require( gplots, keep.source = FALSE );
require( GDD, keep.source = FALSE );

ep.plotDensity = function( data, ylab="density", xlab="x", type="l", col=1:6, ... )
    {
    filtered_data = data[ , apply( !is.na( data ), 2, sum ) > 1 ];
    if ( !is.null( dim(filtered_data) ) )
        {
        x.density = apply( filtered_data, 2, density, na.rm = TRUE );
        all.x = do.call( "cbind", lapply( x.density, function(x) x$x ) );
        all.y = do.call( "cbind", lapply( x.density, function(x) x$y ) );
        matplot( all.x, all.y, ylab = ylab, xlab = xlab, type = type, col = col, ... );
        invisible( list( all.x = all.x, all.y = all.y ) );
        }
    else
        {
        plot.new();
        par( mar = c(0, 0, 0, 0), usr = c(0, 1, 0, 1) );
        text( 0.5, 0.5, "Too many missing values to generate density plot", col = "black", font = 2 );
        }
    }

ep.vis.exp.heatmap = function( dataset, heatmappng, heatmapscalepng, xscale = 1, yscale = 1, lowcolor = "blue", midcolor = "white", hicolor = "red" )
    {
    color = colorpanel( 99, lowcolor, midcolor, hicolor );
    maxvalue = max( dataset, na.rm = TRUE );
    minvalue = min( dataset, na.rm = TRUE );

    GDD( file = heatmappng, type = "png8", width = xscale * ncol(dataset), height = yscale * nrow(dataset) );
    par( mar = c( 0, 0, 0 ,0) );
    image( t(dataset), col = color, zlim = range( minvalue, maxvalue ), axes = FALSE, xlab = "", ylab = "" );

    na_dataset = ifelse(is.na(t(dataset)), 1, NA)
    image( na_dataset, axes = FALSE, xlab = "", ylab = "", col = "#f6ff00", add = TRUE)
    dev.off();

    # png( filename = heatmapscalepng, width = 200, height = 50, pointsize = 12, colortype = "pseudo.cube" );
    # scale = matrix( 0:100 );
    # par( mar = c( 2, 1, 0.5, 1 ) );
    # image( (scale * ( maxvalue - minvalue ) / 100) + minvalue, y = 1, z = scale, col = color, zlim = range(0, 100), yaxt = "n", xlab = "", ylab = "", bty = "n", xaxs = "r", mgp = c( 1.5, 0.5, 0 ) )
    # dev.off();
    }

ep.vis.exp.histogram = function( dataset, histplotpng )
    {
    color = rainbow( ncol(dataset) );
    png( filename = histplotpng, width = 500, height = 350, pointsize = 12, colortype = "pseudo.cube" );
    par( mar= c(2.0, 2.0, 0.2, 0.2), mgp = c( 0.9, 0.1, 0 ), tcl = -0.25, cex.axis = 0.7 );
    ep.plotDensity( dataset, col = color[1:ncol(dataset)], lwd = 2, lty = "solid", xlab = "values", ylab = "density" );
    dev.off();
    }

ep.vis.exp.lineplot = function( dataset, lineplotpng )
    {
    color = rainbow( ncol(dataset) );
    png( filename = lineplotpng, width = 500, height = 350, pointsize = 12, colortype = "pseudo.cube" );
    par( mar= c(2.0, 2.0, 0.2, 0.2), mgp = c( 0.9, 0.1, 0 ), tcl = -0.25, cex.axis = 0.7 );
    boxplot( data.frame(dataset), col = color[1:ncol(dataset)], names = FALSE, xlab = "sample", ylab = "values" );
    dev.off();
    }

ep.vis.exp.legend = function( dataset, legendpng )
    {
    color = rainbow( ncol(dataset) );
    png( filename = legendpng, width = 250, height = 350, pointsize = 12, colortype = "pseudo.cube" );
    plot.new();
    par( mar = c(0, 0, 0, 0), usr = c(0, 1, 0, 1) );
    legend( x = "topleft", inset = 0, legend = colnames(dataset), cex = 0.8, col = color[1:ncol(dataset)], lwd = 3, lty = "solid", bty="n");
    dev.off();
    }

ep.exp.getstats = function( dataset )
    {
    ds_mean = mean(dataset, na.rm = TRUE);
    ds_stdev = sd(as.vector(dataset), na.rm = TRUE);
    ds_median = median(dataset, na.rm = TRUE);
    ds_min = min(dataset, na.rm = TRUE);
    ds_max = max(dataset, na.rm = TRUE);
    
    return(c(ds_mean, ds_stdev, ds_median, ds_min, ds_max));
    }
